package com.mydemo.likedy.controller.UserController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mydemo.likedy.Service.FavoritesService;
import com.mydemo.likedy.Service.QiNiuFileService;
import com.mydemo.likedy.Service.UserService;
import com.mydemo.likedy.Utils.Result;
import com.mydemo.likedy.config.QiNiuConfig;
import com.mydemo.likedy.holder.UserHolder;
import com.mydemo.likedy.pojo.dto.UpdateUserDTO;
import com.mydemo.likedy.pojo.entity.Favorites;
import com.mydemo.likedy.pojo.vo.BasePage;
import com.mydemo.likedy.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/luckyjourney/customer")
public class UserController {

    @Autowired
    FavoritesService favoritesService;

    @Autowired
    QiNiuConfig qiNiuConfig;
    @Autowired
    UserService userService;

    /**
     * 获取所有的收藏夹
     *
     * @return
     */
    @GetMapping("/favorites")
    public Result listFavorites() {
        final Long userId = UserHolder.get();
        List<Favorites> favorites = favoritesService.listByUserId(userId);
        return Result.success(favorites, userId);
    }


    /**
     * 添加/修改收藏夹
     *
     * @param favorites
     * @return
     */
    @PostMapping("/favorites")
    public Result saveOrUpdateFavorites(@RequestBody @Validated Favorites favorites) {
        final Long userId = UserHolder.get();
        final Long id = favorites.getId();
        favorites.setUserId(userId);
        final int count = (int) favoritesService.count(new LambdaQueryWrapper<Favorites>()
                .eq(Favorites::getName, favorites.getName()).eq(Favorites::getUserId, userId).ne(Favorites::getId, favorites.getId()));
        if (count == 1) {
            return Result.error("已存在相同名称的收藏夹");
        }
        favoritesService.saveOrUpdate(favorites);
        return Result.success(id != null ? "修改成功" : "添加成功");
    }

    /**
     * 获得指定收藏信息
     *
     * @param id
     * @return
     */
    @GetMapping("/favorites/{id}")
    public Result getFavorites(@PathVariable Long id) {
        return Result.success(favoritesService.getById(id), UserHolder.get());
    }


    /**
     * 删除收藏夹
     *
     * @param id
     * @return
     */
    @DeleteMapping("/favorites/{id}")
    public Result deleteFavorites(@PathVariable Long id) {

        favoritesService.remove(id, UserHolder.get());
        return Result.success("删除成功");
    }

    /**
     * 获取个人信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping("/getInfo/{userId}")
    public Result getInfo(@PathVariable Long userId) {
        UserVO userVO = userService.getInfo(userId);
        return Result.success(userVO, UserHolder.get());
    }

    @GetMapping("/getInfo/")
    public Result getInfotest() {
        UserVO userVO = userService.getInfo(UserHolder.get());
        return Result.success(userVO, UserHolder.get());
    }

    @PutMapping
    public Result updateUser(@RequestBody @Validated UpdateUserDTO updateUserDTO) {
        userService.updateUser(updateUserDTO);
        return Result.success("修改成功");
    }

    /**
     * 获取用户上传头像的token
     *
     * @return
     */
    @GetMapping("/avatar/token")
    Result getImageToken() {
        return Result.success(qiNiuConfig.imageUploadToken());
    }


    /**
     * 订阅分类,传入标签分类 1,2,3
     */
    @PostMapping("/subscribe")
    public Result subscribe(@RequestParam(required = false) String types) {
        final HashSet<Long> typeSet = new HashSet<>();
        String msg = "取消订阅";
        if (!ObjectUtils.isEmpty(types)) {
            for (String s : types.split(",")) {
                typeSet.add(Long.parseLong(s));
            }
            msg = "订阅成功";
        }
        userService.subscribe(typeSet);
        return Result.success(msg);
    }

    /**
     * 关注/取关
     *
     * @param followsUserId
     * @return
     */
    @PostMapping("/follows")
    public Result follows(@RequestParam Long followsUserId) {

        return Result.success(userService.follows(followsUserId) ? "已关注" : "已取关");
    }

    /**
     * 获取粉丝
     *
     * @param basePage
     * @param userId
     * @return
     */
    @GetMapping("/fans")
    public Result getFans(BasePage basePage, Long userId) {
        return Result.success(userService.getFans(userId, basePage), UserHolder.get());
    }

    /**
     * 获取用户订阅的分类
     *
     * @return
     */
    @GetMapping("/subscribe")
    public Result listSubscribeType() {
        return Result.success(userService.listSubscribeType(UserHolder.get()), UserHolder.get());
    }

    // 获取用户没订阅的分类
    @GetMapping("/noSubscribe")
    public Result listNoSubscribeType() {
        return Result.success(userService.listNoSubscribeType(UserHolder.get()), UserHolder.get());
    }
}


